#include "registerpage.h"
#include "ui_registerpage.h"
#include "MessageType.h"
#include "network.h"
#include "tcpsocketsingleton.h"
#include <QVBoxLayout>

registerPage::registerPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::registerPage)
{
    ui->setupUi(this);

    this->setWindowTitle("医生端");
    this->setWindowIcon(QIcon(":doctorimg/img/doctorIcon.png"));

    // 设置主窗口背景及控件样式
    this->setStyleSheet(
        "QWidget { background-color: rgba(255, 240, 245, 0.95); }" // 整体背景为浅粉色
        "QLabel { color: #880E4F; font-size: 24px; font-weight: bold; }" // 增大字体为24px
        "QLineEdit { min-height: 40px; background-color: rgba(255, 255, 255, 0.9); "
        "border: 2px solid #F48FB1; padding: 12px; border-radius: 10px; font-size: 20px; color: black; }" // 调整输入框
        "QPushButton { min-width: 100px; min-height: 40px; background-color: #F8BBD0; color: white; border: 2px solid #F48FB1; "
        "padding: 15px 30px; border-radius: 12px; font-size: 22px; font-weight: bold; }" // 按钮背景、字体及大小设置
        "QPushButton:hover { background-color: #F48FB1; }" // 悬停时按钮背景变深粉色
    );

    // 主布局设置
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setAlignment(Qt::AlignCenter); // 垂直布局居中
    mainLayout->setSpacing(20); // 控件间距

    // 顶部标题部分
    QWidget *titleWidget = new QWidget(this);
    QGridLayout *titleLayout = new QGridLayout(titleWidget);
    titleLayout->addWidget(ui->label_8, 0, 0, 1, 2, Qt::AlignCenter); // 添加标题标签
    mainLayout->addWidget(titleWidget);

    // 个人信息布局
    QGridLayout *infoLayout = new QGridLayout();
    infoLayout->setSpacing(15); // 控件间距

    // 添加标签和输入框
    infoLayout->addWidget(ui->label, 0, 0); // 姓名标签
    infoLayout->addWidget(ui->lineEdit_name, 0, 1); // 姓名输入框
    infoLayout->addWidget(ui->label_3, 1, 0); // 年龄标签
    infoLayout->addWidget(ui->lineEdit_age, 1, 1); // 年龄输入框
    infoLayout->addWidget(ui->label_2, 2, 0); // 性别标签
    infoLayout->addWidget(ui->lineEdit_sex, 2, 1); // 性别输入框
    infoLayout->addWidget(ui->label_5, 3, 0); // 家庭地址标签
    infoLayout->addWidget(ui->lineEdit_address, 3, 1); // 家庭地址输入框
    infoLayout->addWidget(ui->label_4, 4, 0); // 手机号标签
    infoLayout->addWidget(ui->lineEdit_phone, 4, 1); // 手机号输入框
    infoLayout->addWidget(ui->label_6, 5, 0); // 密码标签
    infoLayout->addWidget(ui->lineEdit_password, 5, 1); // 密码输入框
    infoLayout->addWidget(ui->label_7, 6, 0); // 部门标签
    infoLayout->addWidget(ui->lineEdit_department, 6, 1); // 部门输入框

    mainLayout->addLayout(infoLayout); // 将信息布局添加到主布局

    // 保存和退出按钮布局
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(ui->pushButton); // 添加保存按钮
    buttonLayout->addWidget(ui->pushButton_2); // 添加退出按钮
    mainLayout->addLayout(buttonLayout); // 将按钮布局添加到主布局

    // 为注册按钮设置快捷键 Enter
    ui->pushButton->setShortcut(Qt::Key_Return);

    // 设置主布局
    this->setLayout(mainLayout);

    tcpSocket = TcpSocketSingleton::instance().socket();

    // // 断开之前所有处理函数的连接
    // disconnect(tcpSocket, &QTcpSocket::readyRead, nullptr, nullptr);

    connect(tcpSocket, &QTcpSocket::readyRead, this, [this]() {
        // 处理注册服务器返回的值,此处服务器无返回值
        // .......
    });
}

registerPage::~registerPage()
{
    delete ui;
}

void registerPage::on_pushButton_clicked()
{
    QString name = ui->lineEdit_name->text();
    QString password = ui->lineEdit_password->text();
    QString sex = ui->lineEdit_sex->text();
    QString age = ui->lineEdit_age->text();
    QString phone = ui->lineEdit_phone->text();
    QString department = ui->lineEdit_department->text();
    QString address = ui->lineEdit_address->text();

    // 向服务器发送插入请求
    QMap<QString, QString> data;
    data.insert("type", DOCTORUSRDATABASE);
    data.insert("databaseoperator", QString::number(INSERT));
    data.insert("name", name);
    data.insert("sex", sex);
    data.insert("age", age);
    data.insert("phone", phone);
    data.insert("address", address);
    data.insert("passward", password);
    data.insert("departmentname", department);

    // 转换为 QByteArray 并发送
    QByteArray sendData = mapToByteArrayCustom(data);
    if (tcpSocket->state() == QAbstractSocket::ConnectedState) {
        tcpSocket->write(sendData);
    } else {
        QMessageBox::warning(this, "Socket Error 2", "Socket not connected.");
    }
    QMessageBox::information(this, "注册", "注册成功");
    this->close();
}


void registerPage::on_pushButton_2_clicked()
{
    this->close();
}

